﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class k2 : MonoBehaviour
{
    public GameObject backGround2;
#if UNITY_ANDROID
    private int keyTouchCount = 0;//触摸到当前key的touch数量
#endif
    // Start is called before the first frame update
    void Start()
    {
        Input.multiTouchEnabled = true;
        backGround2.SetActive(false);
        GlobalSettings.Instance.k2Pos = GetComponent<Transform>().position;
    }

    // Update is called once per frame
    void Update()
    {
#if UNITY_ANDROID
        if (GlobalSettings.Instance.isPaused == false)
        {
            keyTouchCount = 0;
            Camera cam = Camera.main;
            for (int i = 0; i < Input.touchCount; i++)
            {
                Ray ray = cam.ScreenPointToRay(Input.GetTouch(i).position); // 这是起点产生的Ray
                RaycastHit hit; // 这是用来检测碰撞的Ray，如果有则存入hit内
                if (Physics.Raycast(ray, out hit))
                {
                    string hitName = hit.transform.name;
                    if (hitName == "k2")
                    {
                        keyTouchCount++;
                        if (Input.GetTouch(i).phase == TouchPhase.Began)
                        {
                            k2Press();
                            backGround2.SetActive(true);
                        }
                    }
                }
            }
            if (keyTouchCount == 0)
            {
                GlobalSettings.Instance.k2Holding = false;
                backGround2.SetActive(false);
            }
            else
            {
                GlobalSettings.Instance.k2Holding = true;
            }
        }
#else
        if (GlobalSettings.Instance.isPaused == false)
        {
            if (Input.GetKeyDown(GlobalSettings.Instance.key2))
            {
                k2Press();
                backGround2.SetActive(true);
            }
            if (Input.GetKeyUp(GlobalSettings.Instance.key2))
            {
                backGround2.SetActive(false);
            }
        }
#endif
    }
    public void k2Press()
    {
        if (GlobalSettings.Instance.isStarted == true && GlobalSettings.Instance.k2InZone == true)
        {
            GlobalSettings.Instance.k2Pressed = true;
        }
    }
}
